package com.d4a5.mapping.google2
{
	import com.d4a5.mapping.core.AmfFactory;
	import com.d4a5.mapping.core.AmfObject;
	import com.google.maps.overlays.PolygonOptions;
	import com.google.maps.styles.FillStyle;
	import com.google.maps.styles.GradientStyle;
	import com.google.maps.styles.StrokeStyle;
	
	import flash.geom.Matrix;
	
	import mx.core.mx_internal;
	
	public class wmgOverlayLayerPolygon extends wmgOverlayLayerBaseShape
	{
		
		public function wmgOverlayLayerPolygon( geometry:Object , options:PolygonOptions = null )
		{
			var amf:AmfObject;
			if( geometry is AmfObject ){
				amf = geometry as AmfObject;
			}
			else if( geometry is Array ){
				amf = AmfFactory.Create( AmfFactory.formatGoogle , AmfFactory.typePolygon , geometry , null );
			}
			
			super(amf);
			
			this.setOptions( ( options )? PolygonOptions.merge( [options,PolygonOptions.getDefaultOptions()] ) : PolygonOptions.getDefaultOptions() );
		}
		
		override protected function __redraw( m:Matrix ):void {
			if( m ){
				
				this.graphics.clear();
				this._drawn = false;
				
				var options:PolygonOptions = this.getOptions() as PolygonOptions;
				
				var stroke:StrokeStyle = options.strokeStyle;
				var fill:FillStyle = options.fillStyle;
				var gFill:GradientStyle;
				if( fill ){
					gFill = options.fillStyle.gradient;
				}
				//this.toolTip = options.tooltip;
				
				var drawOutline:Boolean = false;
				if( stroke && stroke.alpha > 0 && stroke.thickness > 0 ){
					this.graphics.lineStyle( stroke.thickness as Number, stroke.color as uint , stroke.alpha as Number , stroke.pixelHinting );
					drawOutline = true;
				}
				
				if( gFill ){
					this.graphics.beginGradientFill( gFill.type , gFill.colors , gFill.alphas , gFill.ratios , gFill.matrix , gFill.spreadMethod , gFill.interpolationMethod , gFill.focalPointRatio );
					amf.drawRingsOnGraphicsNoTranslation( this.graphics , m );
					this.graphics.endFill();
					this._drawn = true;
				}
				else if( fill ){
					this.graphics.beginFill( fill.color as uint , fill.alpha as Number );
					amf.drawRingsOnGraphicsNoTranslation( this.graphics , m );
					this.graphics.endFill();
					this._drawn = true;
				}
				else if( drawOutline ){
					amf.drawRingsOnGraphicsNoTranslation( this.graphics , m );
					this._drawn = true;
				}
			}
			else{
				this.invalidateDisplayList();
			}
		}
		
		
		override public function setOptions(o:Object):void {
			if( o is PolygonOptions && this._options != o ){
				this._options = o;
				if( this._isAdded ){
					this.redraw();
				}
			}
		}
		
		override public function getDefaultOptions() : Object {
			return PolygonOptions.getDefaultOptions();
		}
		override public function mergeOptions( toBeMerged:Array ) : Object {
			return PolygonOptions.merge( toBeMerged );
		}
		
		override public function updateGeometry( geometry:Object ) : void { 
			var amf:AmfObject;
			if( geometry is AmfObject ){
				amf = geometry as AmfObject;
			}
			else if( geometry is Array ){
				amf = AmfFactory.Create( AmfFactory.formatGoogle , AmfFactory.typePolygon , geometry , null );
			}
			
			this.updateAmf( amf );
		}
		
		override public function generalizeAmf( coordsMatrix:Matrix , generalizePixelCount:uint = 2 ) : void {
			this.amf.generalizeRings( coordsMatrix , generalizePixelCount , true );
		}
	}
}